package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.cc.entity.MeetingRoom;
import com.cc.service.MeetingRoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MeetingRoomServlet")
public class MeetingRoomServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method")==null?"list":req.getParameter("method");
        switch ( method){
            case "list":
                list(req,resp);
                break;
            default:
                break;
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MeetingRoomService meetingRoomService = new com.cc.service.impl.MeetingRoomServiceImpl();
        List<MeetingRoom> meetingRooms = meetingRoomService.queryAllMeetingRoom();
        //将数据转成json使用fastjson
        String json = JSON.toJSONString(meetingRooms);
        //将js数据写给客户端
        resp.getWriter().write(json);
//        req.setAttribute("meetingRooms",meetingRooms);
//        req.getRequestDispatcher("/meetingRoom.jsp").forward(req,resp);

    }
}
